package org.visionlibrary.image.geomtric.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.media.jai.TiledImage;


public class DebugImage {

	private static final File DEFAULT_PATH = new File("debug");

	private static boolean enabled = true;

	private static boolean shortClass = true;

	private static boolean descFilename = true;

	private static boolean show = false;

	private static boolean save = true;

	private static ThreadLocal<File> debugFolder = new ThreadLocal<File>();

	public static void info(BufferedImage image, String desc) {
		if (image == null) {
			System.out.println("WARN: image is null");
		}
		if (!isEnabled()) {
			return;
		}
		String filename = desc;
		if (!descFilename || desc == null) {
			filename = getMethodName();
		}

		if (save) {
			if (!getDebugFolder().exists()) {
				getDebugFolder().mkdirs();
			}
			String path = getDebugFolder() + "/" + filename + ".png";
			try {
				ImageIO.write(image, "png", new File(path));
				System.out.println("INFO: Image saved: " + path);
			} catch (IOException e) {
				System.out.println("INFO: Cannot save image: " + path);
				e.printStackTrace(System.out);
			}
		}
		if (show) {
			TestUtils.showImage(image, filename, false);
		}
	}

	private static File getDebugFolder() {
		File file = debugFolder.get();
		if (file == null) {
			return DEFAULT_PATH;
		}
		return file;
	}

	public static void info(TiledImage image, String desc) {
		if (image == null) {
			System.out.println("WARN: image is null");
		}
		info(image.getAsBufferedImage(), desc);
	}

	public static void info(BufferedImage image) {
		info(image, null);
	}

	public static void info(TiledImage image) {
		info(image, null);
	}

	private static String getMethodName() {
		StackTraceElement[] traceElement = new Exception().getStackTrace();
		StackTraceElement elem = null;
		for (StackTraceElement e : traceElement) {
			if (!e.getClassName().equals(DebugImage.class.getName())) {
				elem = e;
				break;
			}
		}
		String className;
		if (shortClass) {
			String name = elem.getClassName();
			className = name.substring(name.lastIndexOf(".") + 1);
		} else {
			className = elem.getClassName();
		}

		return className + "-" + elem.getMethodName() + "("
				+ elem.getLineNumber() + ")";
	}

	public void setShortClass(boolean shortClass) {
		DebugImage.shortClass = shortClass;
	}

	public void setDescFilename(boolean descFilename) {
		DebugImage.descFilename = descFilename;
	}

	public static void setShow(boolean show) {
		DebugImage.show = show;
	}

	public static void setSave(boolean save) {
		DebugImage.save = save;
	}

	public static void setDebugFolder(String debugFolder) {
		DebugImage.debugFolder.set(new File(debugFolder));
	}

	public static void setEnabled(boolean enabled) {
		DebugImage.enabled = enabled;
	}

	public static boolean isEnabled() {
		return enabled;
	}
}
